home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / ownerd.exe / TESTOWNE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-04-16  |  5.0 KB  |  198 lines

  1.  
  2.  
  3. {Program to test the UOwnerDraw unit. Displays two units, one holding
  4. text strings and one hatch styles. Both of these controls are displayed by
  5. the program rather than Windows.
  6.  
  7. Note that although the list box can hold a string, it just a longint to the
  8. code so it does not carry out any memory management. All memory manipulation
  9. MUST be done by the application, as shown here.
  10.  
  11.  
  12.  
  13. 16th April 1992.
  14.  
  15. Rex K. Perkins, CIS 70651,1611
  16.  
  17. }
  18.  
  19.  
  20.  
  21.  
  22. Program TestOwnerDraw;
  23.  
  24. Uses WinTypes, WinProcs, WObjectB, Strings, UOwnerDraw;
  25.  
  26. Const
  27.      AppName='TestOwner';
  28.  
  29.      id_TextListBox=100;
  30.      id_GraphicsListBox=101;
  31.  
  32.  
  33. Type
  34.  
  35.  
  36.   TTestOwnerApplication = object(TApplication)
  37.     Procedure InitMainWindow; virtual;
  38.   End;
  39.  
  40.  
  41.   PGraphicsListBox=^TGraphicsListBox;
  42.   TGraphicsListBox=Object(TOwnerDrawListBox)
  43.  
  44.     Procedure DrawItem(DrawStruct:PDrawItemStruct); Virtual;
  45.        {Draw the actual item in DrawStruct. Just draw it, everything else is
  46.        taken care of}
  47.   End;
  48.  
  49.  
  50.   PTestOwnerWindow = ^TTestOwnerWindow;
  51.   TTestOwnerWindow = object(TWindow)
  52.     TextListBox:POwnerDrawListBox;
  53.     GraphicsListBox:PGraphicsListBox;
  54.  
  55.     Constructor Init(AParent: PWindowsObject; ATitle: PChar);
  56.  
  57.     Destructor Done; Virtual;
  58.  
  59.     Procedure SetUpWindow; Virtual;
  60.  
  61.     Procedure WMDrawItem(Var Msg:TMessage); Virtual wm_First+wm_DrawItem;
  62.  
  63.   End;
  64.  
  65.  
  66. Procedure TTestOwnerApplication.InitMainWindow;
  67. Begin
  68.   MainWindow := New(PTestOwnerWindow, Init(nil, AppName))
  69. End;
  70.  
  71.  
  72. Constructor TTestOwnerWindow.Init(AParent: PWindowsObject; ATitle: PChar);
  73.  
  74. Begin
  75.   TWindow.Init(AParent, ATitle);
  76.   TextListBox:=New(POwnerDrawListBox,Init(@Self,id_TextListBox,10,10,200,200));
  77.   GraphicsListBox:=New(PGraphicsListBox,Init(@Self,id_GraphicsListBox,220,10,200,200));
  78.   EnableKBHandler
  79. End;
  80.  
  81.  
  82.  
  83. Procedure TTestOwnerWindow.SetUpWindow;
  84.  
  85. {Fill up window the list boxes}
  86.  
  87. Var Count:Word;
  88.     TempStr:String;
  89.  
  90. Begin
  91.   TWindow.SetUpWindow;
  92.   If TextListBox<>Nil Then              {Put 20 strings into the text box}
  93.     For Count:=1 To 20 Do
  94.       Begin
  95.         Str(Count,TempStr);
  96.         TempStr:='A string. Number '+TempStr+#0;
  97.         SendMessage(TextListBox^.HWindow,lb_AddString,0,LONGINT(StrNew(@TempStr[1])))  {We can't use the string methods}
  98.       End;
  99.  
  100.   If GraphicsListBox<>Nil Then              {Put some fill styles into the graphics list box}
  101.     Begin
  102.       SendMessage(GraphicsListBox^.HWindow,lb_AddString,0,hs_BDiagonal);
  103.       SendMessage(GraphicsListBox^.HWindow,lb_AddString,0,hs_Cross);
  104.       SendMessage(GraphicsListBox^.HWindow,lb_AddString,0,hs_DiagCross);
  105.       SendMessage(GraphicsListBox^.HWindow,lb_AddString,0,hs_FDiagonal);
  106.       SendMessage(GraphicsListBox^.HWindow,lb_AddString,0,hs_Horizontal);
  107.       SendMessage(GraphicsListBox^.HWindow,lb_AddString,0,hs_Vertical)
  108.     End
  109. End;
  110.  
  111.  
  112. Destructor TTestOwnerWindow.Done;
  113.  
  114. {Free all strings and quit. The list box will not free the strings it's holding,
  115. after all, it does not know it's got any strings! (Different kind of OwnerDraw)}
  116.  
  117. Var Count:Integer;
  118.  
  119. Begin
  120.   If TextListBox<>Nil Then
  121.     Begin
  122.       Count:=TextListBox^.GetCount-1;
  123.       While Count>=0 Do
  124.         Begin
  125.           StrDispose(PCHAR(SendMessage(TextListBox^.HWindow,lb_GetItemData,Count,0)));
  126.           DEC(Count)
  127.         End
  128.     End;
  129.   TWindow.Done
  130. End;
  131.  
  132.  
  133.  
  134.  
  135.     Procedure TTestOwnerWindow.WMDrawItem(Var Msg:TMessage);
  136.  
  137.     {A request to draw an onwner draw control was received. Check which
  138.     list box it is for and forward it.}
  139.  
  140.     Var Dummy:PDrawItemStruct;
  141.  
  142.     Begin
  143.       If PDrawItemStruct(Msg.LParam)<>Nil Then {Ignore invalid parameters!}
  144.         If (PDrawItemStruct(Msg.LParam)^.CtlID=id_TextListBox) AND (TextListBox<>Nil) Then
  145.           TextListBox^.WMDrawItem(PDrawItemStruct(Msg.LParam))
  146.         Else
  147.           If (PDrawItemStruct(Msg.LParam)^.CtlID=id_GraphicsListBox) AND (GraphicsListBox<>Nil) Then
  148.             GraphicsListBox^.WMDrawItem(PDrawItemStruct(Msg.LParam))
  149.     End;
  150.  
  151.  
  152.  
  153.     Procedure TGraphicsListBox.DrawItem(DrawStruct:PDrawItemStruct);
  154.  
  155.     {Draw a filled box as indicated by the ItemData field}
  156.  
  157.     Var NewBrush,OldBrush:HBrush;
  158.  
  159.     Begin
  160.                 {Create a brush and select it}
  161.       If DrawStruct^.ItemID AND $8000 =0 Then  {Test for index=-1}
  162.         Begin
  163.           NewBrush:=CreateHatchBrush(DrawStruct^.ItemData,GetTextColor(DrawStruct^.HDc));
  164.           OldBrush:=SelectObject(DrawStruct^.HDc,NewBrush)
  165.         End;
  166.  
  167.                 {Draw a rectangle}
  168.       Rectangle(DrawStruct^.HDc,DrawStruct^.rcItem.Left,DrawStruct^.rcItem.Top,
  169.                 DrawStruct^.rcItem.Right,DrawStruct^.rcItem.Bottom);
  170.  
  171.                 {Restore the old brush & delete the new one}
  172.       If DrawStruct^.ItemID AND $8000 =0 Then  {Test for index=-1}
  173.         Begin
  174.           SelectObject(DrawStruct^.HDc,OldBrush);
  175.           DeleteObject(NewBrush)
  176.         End
  177.     End;
  178.  
  179.  
  180.  
  181.  
  182.  
  183. Var
  184.  
  185.   TestApp: TTestOwnerApplication;
  186.  
  187. Begin
  188.   TestApp.Init(AppName);
  189.   TestApp.Run;
  190.   TestApp.Done
  191. End.
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.